package com.goods.controller.business;


import com.goods.business.service.HealthService;
import com.goods.common.error.SystemException;
import com.goods.common.model.business.Health;
import com.goods.common.model.system.User;
import com.goods.common.response.ResponseBean;
import com.goods.common.vo.business.HealthVO;
import com.goods.common.vo.system.PageVO;
import com.goods.common.vo.system.UserInfoVO;
import com.goods.system.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/business/health")
public class HealthController {

    @Autowired
    private HealthService healthService;

    @Autowired
    private UserService userService;

    //判断今日是否打卡
    @GetMapping("/isReport")
    public ResponseBean isReport(){
        UserInfoVO info = null;
        try {
            info = userService.info();
        } catch (SystemException e) {
            e.printStackTrace();
        }
        String username = info.getUsername();
        User userByName = userService.findUserByName(username);
        HealthVO healthVO = healthService.isReport(userByName.getId());
        return ResponseBean.success(healthVO);
    }

    //立即打卡
    @PostMapping("/report")
    public ResponseBean report(@RequestBody Health health){
        UserInfoVO info = null;
        try {
            info = userService.info();
        } catch (SystemException e) {
            e.printStackTrace();
        }
        String username = info.getUsername();
        User userByName = userService.findUserByName(username);
        healthService.report(health,userByName.getId());
        return ResponseBean.success();
    }

    //打卡记录
    @GetMapping("/history")
    public ResponseBean<PageVO<HealthVO>> history(@RequestParam(defaultValue = "1") Integer pageNum,
                                                  @RequestParam(defaultValue = "4") Integer pageSize){
        PageVO<HealthVO> pageVO = healthService.history(pageNum,pageSize);
        return ResponseBean.success(pageVO);
    }
}
